package ${allPackage}${servicePackage}.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import com.ant.common.utils.FastMap;
import com.ant.common.utils.LocalBeanUtil;
import com.ant.common.utils.UserThreadLocal;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ${allPackage}${domainPackage}.${domainUpper};
import ${allPackage}${daoPackage}.${domainUpper}Mapper;
import com.ant.common.exceptions.BusinessException;
import ${allPackage}${servicePackage}.${domainUpper}Service;

import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * ${domainDesc}Service实现类
 *
 * @description
 * @create ${createTime}
 */
@Service("${domain}Service")
@Transactional(rollbackFor = Throwable.class)
public class ${domainUpper}ServiceImpl extends ServiceImpl<${domainUpper}Mapper, ${domainUpper}> implements ${domainUpper}Service {

    private final ${domainUpper}Mapper ${domain}Mapper;

    public ${domainUpper}ServiceImpl(${domainUpper}Mapper ${domain}Mapper) {
        this.${domain}Mapper = ${domain}Mapper;
    }

    /**
     * 分页查询列表
     *
     * @param page 页数
     * @param limit 条数
     * @param param 参数
     * @return PageInfo
     */
    @Override
    public PageInfo<${domainUpper}> getList(int page, int limit, FastMap param) {
        QueryWrapper<${domainUpper}> wrapper = new QueryWrapper<>();
        PageHelper.startPage(page, limit);
        List<${domainUpper}> list = ${domain}Mapper.selectList(wrapper);
        return new PageInfo<${domainUpper}>(list);
    }

    /**
     * 分页查询列表
     *
     * @param param 参数
     * @return PageInfo
     */
    @Override
    public List<${domainUpper}> getAllList(FastMap param) {
        QueryWrapper<${domainUpper}> wrapper = new QueryWrapper<>();
        return ${domain}Mapper.selectList(wrapper);
    }

    /**
     * 通过编号查询
     *
     * @param primaryKey
     * @return
     */
    @Override
    @SneakyThrows
    public ${domainUpper} getById(String primaryKey) {
        ${domainUpper} ${domain} = ${domain}Mapper.selectById(primaryKey);
        if (Objects.isNull(${domain})) {
            throw new BusinessException("${domainDesc}编号不正确");
        }
        return ${domain};
    }

    /**
     * 新增信息
     * @param ${domain} ${domain}
     */
    @Override
    @SneakyThrows
    public void saveOne(${domainUpper} ${domain}) {
        LocalBeanUtil.setLoginUserInfo(${domain}, UserThreadLocal.getUser().getUserName(), UserThreadLocal.getUser().getUnitId());
        int count = ${domain}Mapper.insert(${domain});
        if (count != 1){
            throw new BusinessException("${domainDesc}保存失败");
        }
    }

    /**
     * 更新信息
     * @param ${domain} ${domain}
     */
    @Override
    @SneakyThrows
    public void update(${domainUpper} ${domain}) {
        LocalBeanUtil.updateLoginUserInfo(${domain}, UserThreadLocal.getUser().getUserName());
        int count = ${domain}Mapper.updateById(${domain});
        if (count != 1){
            throw new BusinessException("${domainDesc}更新失败");
        }
    }

    /**
     * 删除信息
     * @param primaryKey id
     */
    @Override
    @SneakyThrows
    public void delete(String primaryKey) {
        ${domainUpper} ${domain} = ${domain}Mapper.selectById(primaryKey);
        if (Objects.isNull(${domain})) {
            throw new BusinessException("未通过编号找到${domainDesc}");
        }
        int count = ${domain}Mapper.deleteById(primaryKey);
        if (count != 1){
            throw new BusinessException("${domainDesc}删除失败");
        }
    }
}
